package com.xiaomage.ipcdemo

import android.content.ContentValues
import android.net.Uri
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import kotlinx.android.synthetic.main.activity_content_provider_contact.*

class ContentProviderContactActivity : AppCompatActivity() {
    private val TAG="联系人内容提供者"
    private var uri:Uri?=null
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_content_provider_contact)
        uri=Uri.parse(ContactContentProvider.CONTACT_URI)
        bt_write.setOnClickListener{
            //写入数据
            val contentValue=ContentValues()
            contentValue.put("phonenumber","18321859453")
            contentValue.put("name","小马哥")
            contentResolver.insert(uri,contentValue)
            Log.i(TAG,"插入数据，main进程pid${android.os.Process.myPid()}")
        }
        bt_read.setOnClickListener{
            //读取数据
            tv_content.text=""
            val colum= arrayOf("phonenumber","name")
            val cursor=contentResolver.query(uri,colum,null,null,null)
            while (cursor.moveToNext()){
                val phoneName=cursor.getString(0)
                val name=cursor.getString(1)
                tv_content.text="取出数据了：联系人：$phoneName,姓名：$name"
            }
            cursor.close()
        }
        bt_delete.setOnClickListener{
            contentResolver.delete(uri,"name=?", arrayOf("小马哥"))
        }
    }
}
